cmake_minimum_required(VERSION 3.15)

project(Weila
        DESCRIPTION "Weila SDK with full functions"
        LANGUAGES C)


# 包含主要的头文件
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/external)

message("CMAKE_STATIC_LIBRARY_SUFFIX is ${CMAKE_STATIC_LIBRARY_SUFFIX_C}")

set(dummy_file ${CMAKE_BINARY_DIR}/dummy.c)
set(weila_sdk_lib ${CMAKE_BINARY_DIR}/lib/weila_sdk${CMAKE_STATIC_LIBRARY_SUFFIX_C})
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/obj)

if (${USING_EXTERNAL_OPUS})
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/libraries/opus)
endif()

# linux 下编译
if (${DEV_PLATFORM} STREQUAL "LINUX")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)

    # 是否使用wlsdk测试程序
    option(USE_WLSDK_TEST "test wlsdk" ON)

    # 是否打开协议的内存测试选项
    option(PROTOCOL_DEBUG_MEM "debug memory" OFF)

    set(link_libs wlosal protocol)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/libraries/protocol)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/wlosal)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/wlsdk)

    # 把多个.a合成一个.a文件
    add_custom_target(combined
            COMMAND ${CMAKE_AR} -x $<TARGET_FILE:wlsdk>
            COMMAND ${CMAKE_AR} -x $<TARGET_FILE:wlosal>
            COMMAND ${CMAKE_AR} -x $<TARGET_FILE:protocol>
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/obj
            COMMAND ${CMAKE_AR} -qcs ${weila_sdk_lib} ${CMAKE_BINARY_DIR}/obj/*.o*
            DEPENDS wlsdk wlosal protocol)
else()
    message("go into ${DEV_PLATFORM}_prj.cmake")
    include(${DEV_PLATFORM}_prj.cmake)
endif()

# 最终合成weila_sdk.a
add_library(weila_sdk STATIC IMPORTED GLOBAL)
add_dependencies(weila_sdk combined)
set_target_properties(weila_sdk
        PROPERTIES
        IMPORTED_LOCATION ${weila_sdk_lib})

# 采用这种dummy.c的原因是因为只有实际的target才会执行custom_target的动作，也就是会去实现上面的weila_sdk.a的生成
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    set(dummy_file ${CMAKE_BINARY_DIR}/dummy.c)
    set(dummy_lib_file ${CMAKE_BINARY_DIR}/lib/libdummy_lib.*)
    set(final_lib_file ${weila_sdk_lib})
    set(final_lib_file_destination ${CMAKE_BINARY_DIR}/sdk_release/lib)
    set(test_lib_file_destination ${CMAKE_SOURCE_DIR}/test/sdk_release/lib)
else()
    macro(modifiy_to_winpath linux_path win_path)
        string(REGEX REPLACE "\/" "\\\\" ${win_path} "${linux_path}")
    endmacro()
    modifiy_to_winpath(${CMAKE_BINARY_DIR}/dummy.c dummy_file)
    modifiy_to_winpath(${CMAKE_BINARY_DIR}/lib/libdummy_lib.* dummy_lib_file)
    modifiy_to_winpath(${weila_sdk_lib} final_lib_file)
    modifiy_to_winpath(${CMAKE_BINARY_DIR}/sdk_release/lib final_lib_file_destination)
    modifiy_to_winpath(${CMAKE_BINARY_DIR}/sdk_release/lib test_lib_file_destination)
endif()

# 生成一个空dummy.c文件并生成dummy_lib.a，才会触动custom_target执行
file(GENERATE OUTPUT ${dummy_file}
        CONTENT "void weila_dummy_fun(){};")
add_library(dummy_lib STATIC ${CMAKE_BINARY_DIR}/dummy.c)
target_link_libraries(dummy_lib weila_sdk)

# 删除无用的
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    set(DEL rm)
    set(DEL_FLAG -rf)
    set(CP cp)
else()
    set(DEL del)
    set(DEL_FLAG /F /Q)
    set(CP copy)
endif()

message(warning "${DEL} ${DEL_FLAG} ${dummy_lib_file} ${final_lib_file} ${test_lib_file_destination}")

add_custom_command(TARGET dummy_lib
        POST_BUILD
        COMMAND ${DEL} ${DEL_FLAG} ${dummy_file}
        COMMAND ${DEL} ${DEL_FLAG} ${dummy_lib_file}
        COMMAND ${CP} ${final_lib_file} ${final_lib_file_destination}
        COMMAND ${CP} ${final_lib_file} ${test_lib_file_destination})


if (${USE_WLSDK_TEST})
    add_subdirectory(${CMAKE_SOURCE_DIR}/test)
endif()         